package com.bootdo.goods.service.impl;

import com.bootdo.goods.dao.TGoodsAllocationDao;
import com.bootdo.goods.domain.TGoodsAllocationDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.goods.dao.TGoodsShelfDao;
import com.bootdo.goods.domain.TGoodsShelfDO;
import com.bootdo.goods.service.TGoodsShelfService;
import org.springframework.transaction.annotation.Transactional;


@Service
public class TGoodsShelfServiceImpl implements TGoodsShelfService {
	@Autowired
	private TGoodsShelfDao tGoodsShelfDao;
	@Autowired
	private TGoodsAllocationDao tGoodsAllocationDao;
	
	@Override
	public TGoodsShelfDO get(Long shelfId){
		return tGoodsShelfDao.get(shelfId);
	}
	
	@Override
	public List<TGoodsShelfDO> list(Map<String, Object> map){
		return tGoodsShelfDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return tGoodsShelfDao.count(map);
	}
	
	@Override
	@Transactional
	public int save(TGoodsShelfDO tGoodsShelf,Integer shelfLnum){
		tGoodsShelfDao.save(tGoodsShelf);
		TGoodsAllocationDO tGoodsAllocationDO = null;
		for (int i = 1; i <= shelfLnum; i++) {
			if(i<=2){//下两层为分捡层
				for (int j = 1; j <=tGoodsShelf.getShelfBnum() ; j++) {
					tGoodsAllocationDO=new TGoodsAllocationDO();
					if(j<10){
						tGoodsAllocationDO.setOrderNum(i+"0"+j);
						tGoodsAllocationDO.setAllocationNumber(tGoodsShelf.getShelfNumber()+i+"0"+j);
					}else {
						tGoodsAllocationDO.setOrderNum(i+""+j);
						tGoodsAllocationDO.setAllocationNumber(tGoodsShelf.getShelfNumber()+i+""+j);
					}


					tGoodsAllocationDO.setAllocationStatus("121001");
					tGoodsAllocationDO.setAllocationType("122001");
					tGoodsAllocationDO.setShelfId(tGoodsShelf.getShelfId());
					tGoodsAllocationDO.setAllocationLine(i);
					tGoodsAllocationDao.save(tGoodsAllocationDO);
				}
			}else {
				for (int j = 1; j <=tGoodsShelf.getShelfBnum() ; j++) {
					tGoodsAllocationDO=new TGoodsAllocationDO();
					if(j<10){
						tGoodsAllocationDO.setOrderNum(i+"0"+j);
						tGoodsAllocationDO.setAllocationNumber(tGoodsShelf.getShelfArea()+tGoodsShelf.getShelfLine()+tGoodsShelf.getShelfNumber()+i+"0"+j);
					}else {
						tGoodsAllocationDO.setOrderNum(i+""+j);
						tGoodsAllocationDO.setAllocationNumber(tGoodsShelf.getShelfArea()+tGoodsShelf.getShelfLine()+tGoodsShelf.getShelfNumber()+i+""+j);
					}
					tGoodsAllocationDO.setAllocationStatus("121001");
					tGoodsAllocationDO.setAllocationType("122002");
					tGoodsAllocationDO.setShelfId(tGoodsShelf.getShelfId());
					tGoodsAllocationDO.setAllocationLine(i);
					tGoodsAllocationDao.save(tGoodsAllocationDO);
				}
			}
		}

		return 1;
	}
	
	@Override
	@Transactional
	public int update(TGoodsShelfDO tGoodsShelf){
		System.out.println("shelfId:"+tGoodsShelf.getShelfId());
		Map<String,Object> map = new HashMap<>();
		map.put("shelfId",tGoodsShelf.getShelfId());
		List<TGoodsAllocationDO> list = tGoodsAllocationDao.list(map);
		for (TGoodsAllocationDO t:list
			 ) {
			t.setProviderId(tGoodsShelf.getProviderId());
			tGoodsAllocationDao.update(t);
		}
		return tGoodsShelfDao.update(tGoodsShelf);
	}
	
	@Override
	public int remove(Long shelfId){
		return tGoodsShelfDao.remove(shelfId);
	}
	
	@Override
	public int batchRemove(Long[] shelfIds){
		return tGoodsShelfDao.batchRemove(shelfIds);
	}
	
}
